var login;
//手机验证码
var waits=60;
$(function () {
   login = new Vue({
       el:'#loginController',
       data:{
           type:1,
           account:{
               phone:'',
               code:'',
               pwd:''
           },
           codeEnable:true,
           codeValue:'获取验证码'
       },
       methods:{
           changeType:function (type) {
               this.type = type;
           },
           getCode:function () {
               var that = this;
               if (that.codeEnable){
                   if (!that.account.phone){
                       sy.msg('请输入手机号!');
                       return;
                   }
                   if (!sy.isPhone(that.account.phone)){
                       sy.msg('请输入正确的手机号!');
                       return;
                   }

                   sy.postApi('/api/account/getMobileMsg',{
                       phone:that.account.phone,
                       type:3
                   },function (data) {
                        //请求
                       that.calCodeTime();
                   })

               }
           },
           calCodeTime:function () {//计算倒计时
               var that = this;
               if(waits==0){
                   that.codeEnable = true;
                   that.codeValue = '获取验证码'
               }else{
                   that.codeEnable = false;
                   that.codeValue = waits+" s";
                   waits--;
                   setTimeout(function(){
                       that.calCodeTime();
                   },1000)
               }
           },
           loginByPwd:function () {
               var that = this;
               if (that.type != 2){
                   sy.msg('登录方式错误!');
                   return;
               }
               var account = that.account;
               if (!account.phone){
                   sy.msg('请输入手机号!');
                   return;
               }
               if (!sy.isPhone(account.phone)){
                   sy.msg('请输入正确的手机号!');
                   return;
               }
               if (!account.pwd){
                   sy.msg('请输入密码!');
                   return;
               }
                //请求登录
               sy.post("/web/login/doLogin",{
                   account:account.phone,
                   password:hex_md5(account.pwd),
                   autoLogin:false
               },function (data) {
                   if (data.code == 0){
                       if (_back_url){
                           location.href = _back_url;
                       }else{
                           location.href = sy.path+'/web';
                       }

                   }else{
                       sy.msg(data.msg);
                   }
               })
           },
           loginByCode:function () {
               var that = this;
               if (that.type != 1){
                   sy.msg('登录方式错误!');
                   return;
               }
               var account = that.account;
               if (!account.phone){
                   sy.msg('请输入手机号!');
                   return;
               }
               if (!sy.isPhone(account.phone)){
                   sy.msg('请输入正确的手机号!');
                   return;
               }
               if (!account.code){
                   sy.msg('请输入验证码!');
                   return;
               }

               //请求登录
               sy.post("/web/login/doLoginByCode",{
                   account:account.phone,
                   code:account.code
               },function (data) {
                   if (data.code == 0){
                       if (_back_url){
                           location.href = _back_url;
                       }else{
                           location.href = sy.path+'/web';
                       }

                   }else{
                       sy.msg(data.msg);
                   }
               })
           }
       }
   });
});